﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Win32;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Threading;
using System.Collections;

public partial class Management_AnnManagment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridViewDataBind();
        }
    }
    protected void GridViewDataBind()
    {
        DBaccess db = new DBaccess();
        DataSet ds = new DataSet();
        DataSet ds1 = new DataSet();
        ArrayList drarr = new ArrayList();
        ArrayList recoarr = new ArrayList();
        //DataSet ds2 = new DataSet();
        String getCid = HttpContext.Current.Request.QueryString["cid"];
        int i;
        ds = db.GetDataSet("select aid 序号,announcement 公告内容,time 上传时间 from mr_announcement");
        DataTable dt = new DataTable();
        dt.Columns.Add("序号");
        dt.Columns.Add("公告内容");
        dt.Columns.Add("上传时间");
        dt.Columns.Add("删除");
        for (i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = ds.Tables[0].Rows[i]["序号"];
            dr[1] = ds.Tables[0].Rows[i]["公告内容"];
            dr[2] = ds.Tables[0].Rows[i]["上传时间"];
            dt.Rows.Add(dr);
        }

        GridView1.DataSource = dt;

        GridView1.AllowPaging = true;
        GridView1.PageSize = 5;
        GridView1.DataBind();
        //GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
        //GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
        GridView1.Columns[1].ItemStyle.Width = 200;
        /*SqlDataReader last_logn_time = db.GetReader("select last_logn_time from mr_member where mid = '" + Session["mid"] + "'");
        if (last_logn_time.Read())
        {
            string lasttime = last_logn_time.GetValue(0).ToString();
            SqlDataReader count = db.GetReader("select count(*) from mr_resource where upload_time >'" + lasttime + "'");
            if (count.Read())
            {
                HyperLink24.Text = "最新资源（" + count.GetValue(0).ToString() + "）";
            }
            else
                HyperLink24.Text = "最新资源（0）";
        }*/
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.Columns[0].ControlStyle.Width = 20;
        GridView1.Columns[1].ControlStyle.Width = 50;
        GridView1.Columns[2].ControlStyle.Width = 40;
        GridView1.Columns[3].ControlStyle.Width = 200;
        GridView1.Columns[4].ControlStyle.Width = 50;
        GridViewDataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.Columns[0].ControlStyle.Width = 20;
        GridView1.Columns[1].ControlStyle.Width = 50;
        GridView1.Columns[2].ControlStyle.Width = 40;
        GridView1.Columns[3].ControlStyle.Width = 200;
        GridView1.Columns[4].ControlStyle.Width = 50;
        GridViewDataBind();
    }
    protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "delete")
        {
            //GridViewRow row  =   null ;
            //Control cmdControl  =  e.CommandSource  as  Control;
            //row  =  cmdControl.NamingContainer  as  GridViewRow;
            //int id = Convert.ToInt32(GridView1.Rows[row.RowIndex].Cells[0].Text);
            //string text;
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow selectedRow = GridView1.Rows[index];
            TableCell contactMid = selectedRow.Cells[0];
            string contact = contactMid.Text;
            DBaccess db = new DBaccess();
            int deleteview = db.ExecSql("delete from mr_announcement where aid = '" + contact + "'");
            Response.Redirect("./AnnManagment.aspx");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DBaccess db = new DBaccess();
        string ann = TextBox1.Text;
        string sql = "select count(*) from mr_announcement";
        SqlDataReader read = db.GetReader(sql);
        read.Read();
        int count = int.Parse(read.GetValue(0).ToString()) + 1;
        string time = DateTime.Now.ToString("T");
        string sql1 = "insert mr_announcement(aid,announcement,time) values('" + count + "','" + ann + "','" + time + "') ";
        if (db.ExecSql(sql1) != 0)
        {
            Response.Write("<script>alert('上传成功!');</script>");
            TextBox1.Text = "";
        }
        else
        {
            Response.Write("<script>alert('上传出错!');</script>");
        }
        Response.Redirect("./AnnManagment.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Session["search"] = TextBox7.Text;
        Response.Redirect("./Search.aspx");
    }
}